package com.kexin.common.config;


import com.kexin.common.interceptor.AppLoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;


@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

	/** 项目访问跟路径 **/
	@Value("${webApp.baseUrl}")
	private String baseUrl;

	private static WebAppConfigurer webAppConfigurer;

	@PostConstruct
	public void init() {
		webAppConfigurer = this;
		webAppConfigurer.baseUrl = baseUrl;
	}

    @Autowired
	private AppLoginInterceptor appLoginInterceptor;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		List<String> interceptPatterns=new ArrayList<>();
		interceptPatterns.add("/**");
		String[] excludeSwagger = {"/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"};
		List<String>excludePatterns=new ArrayList<>();
		excludePatterns.add("/error");
		excludePatterns.add("/static/**");
		excludePatterns.add("/couponOrder/result");
		excludePatterns.add("/couponOrder/usedResult");
		excludePatterns.add("/couponOrder/handleRefund");
		excludePatterns.add("/coupon/list");
		excludePatterns.add("/coupon/one");
		registry.addInterceptor(appLoginInterceptor)
				.addPathPatterns(interceptPatterns)
				.excludePathPatterns(excludePatterns)
				.excludePathPatterns(excludeSwagger);

	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("swagger-ui.html")
			.addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**")
			.addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	public static String getBaseUrl() {
		return webAppConfigurer.baseUrl;
	}
}

